<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-04 16:34
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Repositories;


use App\Models\UserRoyalty;
use App\Modules\Employee\Criterias\getServiceListCriteria;
use App\Modules\Employee\Validators\EmployeeRoyaltyValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;

class EmployeeRoyaltyRepository extends BaseRepository
{
	public function model()
	{
		return UserRoyalty::class;
	}

	public function validator()
	{
		return EmployeeRoyaltyValidator::class;
	}

	/**
	 * 设置和修改 用户服务提成
	 * @param $requestData
	 * @return mixed
	 */
	public function add($data)
	{
		return $this->model->create($data);
	}


	/**
	 * 获取某理发师的对应的服务提成
	 * @param $barberId
	 * @param $serviceId
	 * @return mixed
	 */
	public function getBarberRoyaltyInfo($barberId, $serviceId)
	{
		return $this->findWhere(['user_id' => $barberId, 'service_id' => $serviceId, 'status' => 1])->first();
	}

	/**
	 * 设置提成
	 * @param $user_id
	 * @param $store_id
	 * @param $service_id
	 * @param $royalty
	 * @param $assistant_royalty
	 * @param int $status
	 * @return mixed
	 */
	public function setRoyalty($user_id, $store_id, $service_id, $royalty, $assistant_royalty, $status = 1)
	{
		return $this->updateOrCreate([
			'user_id'    => $user_id,
			'store_id'   => $store_id,
			'service_id' => $service_id],[
			'royalty'           => $royalty,
			'assistant_royalty' => $assistant_royalty,
			'status'            => $status,]);
	}

	/**
	 * 删除理发师
	 * @param $store_id
	 * @param $user_id
	 * @return int
	 */
	public function delBarber($store_id, $user_id)
	{

		return $this->deleteWhere(['user_id' => $user_id, 'store_id' => $store_id]);
	}

	public function delService($service_id)
	{
		return $this->deleteWhere(['service_id' => $service_id]);
	}


	/**
	 * 获取理发师服务提成列表
	 * @param $storeId
	 * @param $userId
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getServiceListByStoreIdAndUserId($userId, $storeId, $status)
	{
		return $this->pushCriteria(new getServiceListCriteria($userId, $storeId, $status))->get();
	}


	/**
	 * 验证理发师和服务店铺是否匹配
	 * @param $userId
	 * @param $serviceId
	 * @param $storeId
	 * @return mixed
	 */
	public function checkBarberServiceIsSuited($userId,$serviceId,$storeId){
		return $this->model->where(['user_id'=>$userId,'service_id'=>$serviceId,'store_id'=>$storeId])->first();
	}
}